package com.example.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author ZZZ
 * @Date 2018年1月23日
 * @Description 在普通工具类里获取spring管理的bean
 */
@Component
public class SpringTool {

    private SpringTool() {
    }

    /**
     * Logger for this class
     */
    private static final Logger logger = LoggerFactory.getLogger(SpringTool.class);

    private static ApplicationContext applicationContext;
    private static Environment environment;


    @Autowired
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (SpringTool.applicationContext == null) {
            SpringTool.applicationContext = applicationContext;
            SpringTool.environment = applicationContext.getEnvironment();
            logger.info("ApplicationContext配置成功,applicationContext=" + applicationContext);
        }
    }

    /**
     * @desc 获取当前上下文环境
     * @author ZZZ
     * @date 2019/2/21 0021
     * @version 1.0
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }


    /**
     * @desc 通过name从ApplicationContext中获取Bean
     * @author ZZZ
     * @date 2018/11/14 014
     * @version 1.0
     */
    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }

    /**
     * @desc 通过类型从ApplicationContext中获取Bean
     * @author ZZZ
     * @date 2018/11/14 014
     * @version 1.0
     */
    public static Object getBean(Class clazz) {
        return getApplicationContext().getBean(clazz);
    }

    /**
     * @desc 获取环境变量
     * @author ZZZ
     * @date 2019/4/4 0004
     * @version 1.0
     */
    public static Environment getEnvironment() {
        return environment;
    }

    /**
     * 获取Request
     * @return
     */
    public static HttpServletRequest getRequest() {
        return getServletRequestAttributes().getRequest();
    }

    /**
     * 获取Response
     * @return
     */
    public static HttpServletResponse getResponse() {
        return getServletRequestAttributes().getResponse();
    }

    private static ServletRequestAttributes getServletRequestAttributes() {
        return (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    }

    /**
     * 支持在子线程获取request
     */
    public static void enableSubThreadRequest() {
        ServletRequestAttributes attributes = getServletRequestAttributes();
        RequestContextHolder.setRequestAttributes(attributes, true);
    }


    /**
     * 获取session
     * @return
     */
    public static HttpSession getSession() {
        return getRequest().getSession();
    }


    /**
     * 是否配置Spring环境
     * @return
     */
    public static boolean isSpringSupport() {
        return applicationContext != null;
    }

}  